package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.MerchantsMapper;
import cn.tedu.trading_platform.pojo.dto.merchantsDTO.MerchantLoginParam;
import cn.tedu.trading_platform.pojo.dto.merchantsDTO.MerchantRegParam;
import cn.tedu.trading_platform.pojo.dto.merchantsDTO.MerchantUpdateParam;
import cn.tedu.trading_platform.pojo.entity.Merchant;
import cn.tedu.trading_platform.pojo.vo.MerchantsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiOperationSupport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/merchants/")
@Api(tags = "商家管理模块")
public class MerchantsController {
    @Autowired
    private MerchantsMapper merchantsMapper;
    @PostMapping("reg")
    @ApiOperation("商家注册")
    public JsonResult merchantReg(@Validated @RequestBody MerchantRegParam merchantRegParam) {
        log.debug("开始处理商家注册请求",merchantRegParam);
        int i = merchantsMapper.selectByName(merchantRegParam.getMerchantsName());
        if(i!=0){
            return new JsonResult(StatusCode.USER_EXIST);
        }
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantRegParam,merchant);
        merchant.setCreatedAt(new Date());
        merchantsMapper.insertMerchants(merchant);
        // 模拟注册逻辑（需替换为实际数据库操作）
        return JsonResult.ok();
    }

    @PostMapping("login")
    @ApiOperation("商家登录")
    public JsonResult merchantLogin(@Validated @RequestBody MerchantLoginParam merchantLoginParam,@ApiIgnore HttpSession session) {
        log.debug("开始处理商家登录请求", merchantLoginParam);
        MerchantsVO merchantsVO = merchantsMapper.selectByNameAndPassword(merchantLoginParam);
        if(merchantsVO==null){
            return new JsonResult(StatusCode.USER_NOT_EXIST_OR_PASSWORD_ERROR);
        }
        session.setAttribute("merchants",merchantsVO);
        return JsonResult.ok(merchantsVO);
    }
    @PostMapping("update")
    @ApiOperation("商家信息修改")
    public JsonResult merchantUpdate(MerchantUpdateParam merchantUpdateParam) {
        log.debug("开始处理商家信息修改请求，用户名：{}", merchantUpdateParam.getUsername());

        // 参数校验
        if (merchantUpdateParam.getUsername() == null) {
            return new JsonResult(StatusCode.USER_nOT_UPDATE);
        }

        // 模拟信息更新逻辑（需身份验证和数据库操作）
        return JsonResult.ok("商家信息修改成功");
    }
}